package com.szgr.baoyin.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.szgr.baoyin.MainActivity;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.model.CardMessage;
import com.szgr.baoyin.model.CardType;
import com.szgr.baoyin.model.InvoiceListItem;
import com.szgr.baoyin.utils.Constant;
import com.szgr.baoyin.utils.MyTool;
import com.szgr.baoyin.utils.RLog;
import com.szgr.baoyin.utils.ToastUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.List;

public class ApplyCardDetailActivity extends BaseActivity {

    private CardMessage cardMsg;
    private InvoiceListItem invoice;
    private CardType cardType;
    private String pay_sn;
    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case Constant.REQUEST_SUCCESS:
                    Intent intent = new Intent(ApplyCardDetailActivity.this, WebActivity.class);
                    intent.putExtra("html", (String) msg.obj);
                    startActivity(intent);
                    break;
                case Constant.REQUEST_FAIL:

                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_apply_card_detail);
        super.onCreate(savedInstanceState);
    }

    @Override
    public void initActionBar() {
        actionBarTitle.setText("都银卡订单详情");
    }

    @Override
    public void initView() {
        TextView tv_name = (TextView) findViewById(R.id.apply_name);
        TextView tv_id = (TextView) findViewById(R.id.buy_protect_company1);
        TextView tv_phone = (TextView) findViewById(R.id.card_phone);
        TextView tv_company = (TextView) findViewById(R.id.card_company);
        TextView tv_companyAddress = (TextView) findViewById(R.id.card_address);
        TextView tv_emailAddress = (TextView) findViewById(R.id.buy_protect_address1);
        TextView tv_email = (TextView) findViewById(R.id.buy_protect_faren1);

        TextView tv_01 = (TextView) findViewById(R.id.invoice_01);
        TextView tv_02 = (TextView) findViewById(R.id.invoice_02);
        TextView tv_03 = (TextView) findViewById(R.id.invoice_03);
        TextView tv_04 = (TextView) findViewById(R.id.invoice_04);
        TextView tv_05 = (TextView) findViewById(R.id.invoice_05);
        TextView tv_06 = (TextView) findViewById(R.id.invoice_06);
        TextView tv_07 = (TextView) findViewById(R.id.invoice_07);
        TextView tv_08 = (TextView) findViewById(R.id.invoice_08);
        TextView tv_09 = (TextView) findViewById(R.id.invoice_09);
        TextView tv_10 = (TextView) findViewById(R.id.invoice_10);

        TextView tv_money = (TextView) findViewById(R.id.card_money);
        TextView tv_cardName = (TextView) findViewById(R.id.pay_card_type);
        TextView tv_level = (TextView) findViewById(R.id.card_level);
        TextView tv_cardMinus1 = (TextView) findViewById(R.id.card_minus1);
        TextView tv_cardMinus2 = (TextView) findViewById(R.id.card_minus2);
        TextView tv_all = (TextView) findViewById(R.id.card_all);

        findViewById(R.id.my_order_detail_submit1).setOnClickListener(this);
        findViewById(R.id.back).setOnClickListener(this);

        Intent intent = getIntent();
        if (intent != null && intent.hasExtra("bundle")) {
            Bundle bundle = intent.getBundleExtra("bundle");
            cardMsg = (CardMessage) bundle.getSerializable("cardMsg");
            invoice = (InvoiceListItem) bundle.getSerializable("invoice");
            cardType = (CardType) bundle.getSerializable("cardType");
            pay_sn = bundle.getString("pay_sn");

            tv_name.setText(cardMsg.getEnterprise_author());
            tv_id.setText(cardMsg.getEnterprise_author_idcard());
            tv_phone.setText(cardMsg.getEnterprise_phone());
            tv_company.setText(cardMsg.getEnterprise_name());
            tv_companyAddress.setText(cardMsg.getEnterprise_address());
            tv_emailAddress.setText(cardMsg.getEnterprise_address_recv());
            tv_email.setText(cardMsg.getEnterprise_email());

            tv_01.setText(invoice.getInv_company());
            tv_02.setText(invoice.getInv_code());
            tv_03.setText(invoice.getInv_reg_addr());
            tv_04.setText(invoice.getInv_reg_phone());
            tv_05.setText(invoice.getInv_reg_bname());
            tv_06.setText(invoice.getInv_reg_baccount());
            tv_07.setText(invoice.getInv_rec_name());
            tv_08.setText(invoice.getInv_rec_mobphone());
            tv_09.setText(invoice.getInv_rec_province());
            tv_10.setText(invoice.getInv_goto_addr());

            tv_cardName.setText(cardType.getCard_name());
            tv_money.setText(cardType.getCard_limit());
            tv_all.setText(cardType.getCard_limit());
            tv_level.setText("Lv " + cardType.getCard_id());
            List<CardType.CardConfig> card_config = cardType.getCard_config();
            for (CardType.CardConfig cc : card_config) {
                if("12".equals(cc.getStore_goods_class())){
                    tv_cardMinus1.setText("配件 " + MyTool.formatDouble(Double.parseDouble(cc.getCard_discount()) * 10) + "折");
                }else if("13".equals(cc.getStore_goods_class())){
                    tv_cardMinus2.setText("维保 " + MyTool.formatDouble(Double.parseDouble(cc.getCard_discount()) * 10) + "折");
                }
            }
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.action_bar_left:
                finish();
                break;
            case R.id.my_order_detail_submit1:
                submitPay();
                break;
            case R.id.back:

                break;
        }
    }

    private void submitPay() {
        StringRequest sr = new StringRequest(Request.Method.POST, Constant.URL_PAY_CARD + "&key=" + MyApplication.getmInstance().getKey() + "&pay_sn=" + pay_sn,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        RLog.s("--pay--->" + s);
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONObject datas = jsonObject.getJSONObject("datas");
                            String error = datas.getString("error");
                            mHandler.obtainMessage(Constant.REQUEST_FAIL, error).sendToTarget();
                        } catch (JSONException e) {
                            mHandler.obtainMessage(Constant.REQUEST_SUCCESS, s).sendToTarget();
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        MyApplication.getmInstance().addToRequestQueue(sr);
    }
}
